home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscPaperViewPalette / MiscPaperView.subproj / MiscPaperView.m < prev    next >
Text File  |  1995-07-20  |  6KB  |  308 lines

  1. //        Written by Thomas Engel Copyright (c) 1995 by Thomas Engel.
  2. //                Version 1.0.  All rights reserved.
  3. //
  4. //        This notice may not be removed from this source code.
  5. //
  6. //    This object is included in the MiscKit by permission from the author
  7. //    and its use is governed by the MiscKit license, found in the file
  8. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  9. //    for a list of all applicable permissions and restrictions.
  10. //    
  11.  
  12. #import <misckit/MiscPaperView.h>
  13.  
  14. #define MISC_PAPERVIEW_VERSION 0
  15. #define MISC_PAPERVIEW_CLASSNAME "MiscPaperView"
  16.  
  17.  
  18. @implementation MiscPaperView
  19.  
  20. + initialize
  21. {
  22.     // Initialize the current version number which is used when archiving 
  23.     // objects. This way we will be able to read all versions if we are 
  24.     // careful.
  25.  
  26.     if( self == [MiscPaperView class] )
  27.         [self setVersion:MISC_PAPERVIEW_VERSION];
  28.         
  29.     return self;
  30. }
  31.  
  32. - initFrame:(const NXRect *)frameRect
  33. {
  34.     // Designated initilizer. We will set allow resizing by default and
  35.     // we will draw in Lightgray if we have to swap 'nil' in.
  36.  
  37.     self = [super initFrame:frameRect];
  38.     if( !self ) return self;
  39.  
  40.     // By default will draw a simple horizontal light gray grid with a dark
  41.     // gray right side border.
  42.  
  43.     gridColor = NX_COLORLTGRAY;
  44.     gridType = Misc_PaperGridHorizontal;
  45.     gridOrigin = Misc_PaperGridStartsUpperLeft;
  46.     gridVertOffset = gridHorOffset = 20;
  47.  
  48.     sidelineColor = NX_COLORDKGRAY;
  49.     sidelineType = Misc_PaperSidelineRight;
  50.     sidelineOffset = 4;
  51.  
  52.     return self;
  53. }
  54.  
  55. - setGridColor:(NXColor)color
  56. {
  57.     gridColor = color;
  58.     return self;
  59. }
  60.  
  61. - (NXColor)gridColor
  62. {
  63.     return gridColor;
  64. }
  65.  
  66. - setGridType:(int)aType withOrigin:(int)theOrigin
  67. {
  68.     gridType = aType;
  69.     gridOrigin = theOrigin;
  70.     return self;
  71. }
  72.  
  73. - (int)gridType
  74. {
  75.     return gridType;
  76. }
  77.  
  78. - (int)gridOrigin
  79. {
  80.     return gridOrigin;
  81. }
  82.  
  83. - setGridSizeVertical:(int)vert horizontal:(int)hor
  84. {
  85.     // Well there has to be some progress so forbit useless offsets
  86.  
  87.     if( vert < 1 ) vert = 1;
  88.     if( hor < 1 ) hor = 1;
  89.  
  90.     gridVertOffset = vert;
  91.     gridHorOffset = hor;
  92.  
  93.     return self;
  94. }
  95.  
  96. - (int)verticalGridSize
  97. {
  98.     return gridVertOffset;
  99. }
  100.  
  101. - (int)horizontalGridSize
  102. {
  103.     return gridHorOffset;
  104. }
  105.  
  106. - setSidelineColor:(NXColor)color
  107. {
  108.     sidelineColor = color;
  109.     return self;
  110. }
  111.  
  112. - (NXColor)sidelineColor
  113. {
  114.     return sidelineColor;
  115. }
  116.  
  117. - setSidelineType:(int)aType
  118. {
  119.     sidelineType = aType;
  120.     return self;
  121. }
  122.  
  123. - (int)sidelineType
  124. {
  125.     return sidelineType;
  126. }
  127.  
  128. - setSidelineOffset:(int)offset
  129. {
  130.     sidelineOffset = offset;
  131.     return self;
  132. }
  133.  
  134. - (int)sidelineOffset
  135. {
  136.     return sidelineOffset;
  137. }
  138.  
  139. - drawSelf:(const NXRect *)rects :(int)rectCount
  140. {    
  141.     float    xMin;
  142.     float    xMax;
  143.     float    yMin;
  144.     float    yMax;
  145.  
  146.     float    offset;
  147.     float    coord;
  148.     int        i;
  149.     int        count;
  150.  
  151.     [super drawSelf:rects :rectCount];
  152.  
  153.     xMin = bounds.origin.x;
  154.     xMax = bounds.origin.x + bounds.size.width - 1;
  155.     yMin = bounds.origin.y;
  156.     yMax = bounds.origin.y + bounds.size.height - 1;
  157.  
  158.     // BUG: Please tell me whats wrong ?
  159.     // Well everybody would think that this should be the right calculation ?
  160.     // At least I do...but somehow you have to adjust the Y values of the bounds
  161.     // because otherwise you'll start 1 pixel too low (out side the view) and
  162.     // you will end 1 pixel too early ????. 
  163.     // Perhaps I missed some kind of stroke adjust ?
  164.  
  165.     yMin += 1;
  166.     yMax += 1;
  167.  
  168.     // Draw the grid first...but lets see where we have to start from
  169.     // and how many lines we need.
  170.  
  171.     PSsetlinewidth( 1.0 );
  172.     NXSetColor( gridColor );
  173.  
  174.     if( gridType & Misc_PaperGridHorizontal )
  175.     {
  176.         count = bounds.size.height / gridVertOffset;
  177.  
  178.         // If we start from the top then the values are different...
  179.  
  180.         if( gridOrigin & Misc_PaperGridStartsLow )
  181.         {
  182.             coord = yMin - 1;
  183.             offset = gridVertOffset;
  184.         }
  185.         else
  186.         {
  187.             coord = yMax + 1;
  188.             offset = -gridVertOffset;
  189.         }
  190.         for( i=0; i<count; i++ )
  191.         {
  192.             coord += offset;
  193.             PSmoveto( xMin, coord );
  194.             PSlineto( xMax, coord );
  195.             PSstroke();
  196.         }
  197.     }
  198.  
  199.     // Now lets do the same for Vertical lines.
  200.  
  201.     if( gridType & Misc_PaperGridVertical )
  202.     {
  203.         count = bounds.size.width / gridHorOffset;
  204.  
  205.         // If we start from the left side then the values are different...
  206.  
  207.         if( gridOrigin & Misc_PaperGridStartsRight )
  208.         {
  209.             coord = xMax + 1;
  210.             offset = -gridHorOffset;
  211.         }
  212.         else
  213.         {
  214.             coord = xMin - 1;
  215.             offset = gridHorOffset;
  216.         }
  217.         for( i=0; i<count; i++ )
  218.         {
  219.             coord += offset;
  220.             PSmoveto( coord, yMin );
  221.             PSlineto( coord, yMax );
  222.             PSstroke();
  223.         }
  224.     }    
  225.  
  226.     // Now draw the borders..
  227.  
  228.     NXSetColor( sidelineColor );
  229.     
  230.     if( sidelineType & Misc_PaperSidelineTop )
  231.     {
  232.         PSmoveto( xMin, yMax - sidelineOffset );
  233.         PSlineto( xMax, yMax - sidelineOffset );
  234.         PSstroke();
  235.     }
  236.  
  237.     if( sidelineType & Misc_PaperSidelineBottom )
  238.     {
  239.         PSmoveto( xMin, yMin + sidelineOffset );
  240.         PSlineto( xMax, yMin + sidelineOffset );
  241.         PSstroke();
  242.     }
  243.  
  244.     if( sidelineType & Misc_PaperSidelineLeft )
  245.     {
  246.         PSmoveto( xMin + sidelineOffset, yMin );
  247.         PSlineto( xMin + sidelineOffset, yMax );
  248.         PSstroke();
  249.     }
  250.  
  251.     if( sidelineType & Misc_PaperSidelineRight )
  252.     {
  253.         PSmoveto( xMax - sidelineOffset, yMin );
  254.         PSlineto( xMax - sidelineOffset, yMax );
  255.         PSstroke();
  256.     }
  257.  
  258.     return self;
  259. }
  260.  
  261. - read:(NXTypedStream *)stream
  262. {
  263.     int  version;
  264.   
  265.     [super read:stream];
  266.     
  267.     version = NXTypedStreamClassVersion( stream, MISC_PAPERVIEW_CLASSNAME );
  268.     
  269.     switch( version )
  270.     {
  271.         case 0:
  272.             gridColor = NXReadColor( stream );
  273.             NXReadType( stream, "i", &gridType );
  274.             NXReadType( stream, "i", &gridOrigin );
  275.             NXReadType( stream, "i", &gridVertOffset );
  276.             NXReadType( stream, "i", &gridHorOffset );
  277.  
  278.             sidelineColor = NXReadColor( stream );
  279.             NXReadType( stream, "i", &sidelineType );
  280.             NXReadType( stream, "i", &sidelineOffset );
  281.             break;
  282.         
  283.         default:
  284.             break;
  285.     }
  286.  
  287.     return self;
  288. }
  289.  
  290. - write:(NXTypedStream *)stream
  291. {
  292.     [super write:stream];
  293.  
  294.     NXWriteColor( stream, gridColor );
  295.     NXWriteType( stream, "i", &gridType );
  296.     NXWriteType( stream, "i", &gridOrigin );
  297.     NXWriteType( stream, "i", &gridVertOffset );
  298.     NXWriteType( stream, "i", &gridHorOffset );
  299.  
  300.     NXWriteColor( stream, sidelineColor );
  301.     NXWriteType( stream, "i", &sidelineType );
  302.     NXWriteType( stream, "i", &sidelineOffset );
  303.  
  304.     return self;
  305. }
  306.  
  307. @end
  308.